<?php

/**
 * 代金卡生产单 ajax类
 * @package  ajax
 * @subpackage  backend
 * @create by allen.qiang
 */
header('Content-Type:text/html;charset=utf-8');
require 'module/base/cardHandler.class.php';

class ajax_cardProduct {

    public function execute() {

        if (isset($_REQUEST['operFlg']) && $_REQUEST['operFlg'] > 0) {

            switch($_REQUEST['operFlg']) {

                case 1: //代金卡生产单列表
                    $this->cardProductList();
                    break;

                case 2: //新增或编辑代金卡生产单
                    $this->cardProductAdd();
                    break;

                case 3: //删除代金卡生产单
                    $this->cardProductDel();
                    break;

                case 4: //审核代金卡生产单
                    $this->auditCard();
                    break;

                case 5: //代金卡入库单列表
                    $this->cardRkdList();
                    break;

                case 6: //新增或编辑代金卡入库单
                    $this->editCardRKD();
                    break;

                case 7: //删除代金卡入库单
                    $this->delCardRKD();
                    break;

                case 8: //审核代金卡入库单
                    $this->auditCardRKD();
                    break;

                case 9: //代金卡详情列表
                    $this->cardDetailList();
                    break;

                case 10:  //批量禁用代金卡
                    $this->batchChangeCardStat();
                    break;

                case 11:  //回滚卡状态
                    $this->cardRollBack();
                    break;


            }

        }

    }

    //获取代金卡生产单列表
        public function cardProductList(){

        global $G_PGLOB;

        //查询条件
        $cardHandler = new cardHandler( $G_PGLOB->DBCA ) ;
        $result_data	  = $cardHandler->searchCardProduct( $_REQUEST ) ;

        //查询结果
        $result_array = array(
            'page'  => $_REQUEST['page'],
            'total' => $_REQUEST['count'],
            'rows'  => array()
        );

        if ($result_data) {
            foreach($result_data as &$cardProductInfo) {

                //操作字符串
                $operatorString = '';

                //代金卡状态字符串
                $cardStatus = '';

                //代金卡入库状态
                $cardStoreStatus = '';

                if( $cardProductInfo['cardType'] == 1 ){
                    $cardType = '虚拟卡';
                }elseif ( $cardProductInfo['cardType'] == 2 ){
                    $cardType = '线上销售实体卡';
                }elseif ( $cardProductInfo['cardType'] == 3 ){
                    $cardType = '线下销售实体卡';
                }

                //查询该生产单下已经入库的代金卡信息
                $sql_insert = 'SELECT startID,endID FROM card_rkd WHERE status=? AND productID=?';
                $result = $G_PGLOB->DBCA->stmt_query( $sql_insert , 'ii' , array(1,$cardProductInfo['productID']) );

                if( $result ){

                    $sum = 0;  //统计已经入库的代金卡数目

                    foreach ( $result as $stored ){
                        $sum += ($stored->endID - $stored->startID + 1);
                    }

                    if( $sum < $cardProductInfo['cardNumber'] ){
                        $cardStoreStatus .= '部分入库';
                    }else{
                        $cardStoreStatus .= '全部入库';
                    }

                }else{
                    $cardStoreStatus .= '未入库';
                }

                if( $cardProductInfo['status'] == 1 ){
                    $operatorString .= '<a href="javascript: void(0);" onclick="cardProductList.edit(' . $cardProductInfo['productID'] . ')">修改</a>&nbsp;'.
                        '<a href="javascript: void(0);" onclick="cardProductList.audit(' . $cardProductInfo['productID'] .',\''.$cardProductInfo['cardName']. '\')">审核</a>';

                    $cardStatus .= '待审核';
                }

                if( $cardProductInfo['status'] == 2  ){

                    if( $cardStoreStatus != '全部入库' ){
                        $operatorString .= '<a href="javascript: void(0);" onclick="cardProductList.putinStore(' . $cardProductInfo['productID'] . ')">入库</a>&nbsp;';
                    }

                    $operatorString .= '<a href="javascript: void(0);" onclick="cardProductList.exportCard(' . $cardProductInfo['productID'] . ')">导出</a>&nbsp;';

                    $operatorString .= '<a href="javascript: void(0);" onclick="cardProductList.detail(' . $cardProductInfo['productID'] . ')">详情</a>';

                    $cardStatus .= '已生产';
                }

                //查询商品筛选器信息
                $search_goodsSelector = 'SELECT * FROM goods_selector WHERE selectID = ?';
                $goodsSelector        = $G_PGLOB->DBCA->stmt_query( $search_goodsSelector , 'i' , $cardProductInfo['goodsSelector'] );

                if( $goodsSelector ){
                    $selectName = $goodsSelector[0]->selectName;
                }else{
                    $selectName = '';
                }

                $result_array['rows'][] = array(
                    'id' 	=> $cardProductInfo['productID'],
                    'cell' 	=> array(
                        $operatorString ,
                        $cardProductInfo['status'] == 1 ? $cardProductInfo['productID'] :
                            '<a href="javascript: void(0);" onclick="cardProductList.detail(' . $cardProductInfo['productID'] . ')">'.$cardProductInfo['productID'].'</a>' ,
                        $cardProductInfo['cardName'],
                        $cardProductInfo['faceValue'],
                        $cardType,
                        $cardProductInfo['cardNumber'],
                        $cardProductInfo['useNumber'],
                        $selectName,
                        $cardProductInfo['startDate'],
                        $cardProductInfo['endDate'],
                        $cardStatus,
                        $cardStoreStatus
                    )
                );
            }
        }

        echo json_encode( $result_array );
    }

    /**
     * 更新或保存代金卡生产单信息
     */
    private function cardProductAdd() {

        global $G_PGLOB;

        /**
         * 代金卡生产单状态：1:待审核   2：生产   3:入库
         */

        $_REQUEST['operator'] = $G_PGLOB->currentUserID;

        //编辑时，检查代金卡状态
        if( $_REQUEST['flg'] == 3 ){

            //接收页面传过来的timeStamp
            $timeStamp = $_REQUEST['timeStamp'];

            //查询代金卡生产单信息
            $card_product = new entity('card_product');
            $card_product->productID = $_REQUEST['productID'];

            if( !$G_PGLOB->DBCA->fetch( $card_product ) ){

                echo json_encode( array( 'code'=>404 , 'message'=>'该代金卡已被删除') );
                return;

            }

            if(  $card_product->status != 1 ){

                echo json_encode( array( 'code'=>404 , 'message'=>'该代金卡状态已改变') );
                return;

            }

            if(  $card_product->timeStamp != $timeStamp ){

                echo json_encode( array( 'code'=>404 , 'message'=>'该代金卡已更新，请刷新页面') );
                return;

            }

        }

        //调用卡管理类中的更新代金卡生产单方法
        $cardHandler = new cardHandler( $G_PGLOB->DBCA ) ;
        $result_array = $cardHandler->editCardProduct( $_REQUEST ) ;

        echo json_encode($result_array);

    }

    /**
     * 删除代金卡生产单信息
     *
     */
    private function cardProductDel () {

        global $G_PGLOB;

        //接收参数
        $delArr = $_REQUEST['delArr'];

        //调用卡管理类中的删除代金卡生产单方法
        $cardHandler  = new cardHandler( $G_PGLOB->DBCA ) ;
        $result_array = $cardHandler->delCardProduct( $delArr ) ;

        echo json_encode($result_array);

    }

    //审核代金卡
    public function auditCard(){

        global $G_PGLOB;

        //调用卡管理类中的审核代金卡生产单方法
        $cardHandler = new cardHandler( $G_PGLOB->DBCA ) ;
        $result_array 		 = $cardHandler->auditCardProduct( $_REQUEST['productID'] ) ;

        echo json_encode($result_array);

    }

    //代金卡入库单列表
    function cardRkdList(){

        global $G_PGLOB;

        //查询条件
        $cardHandler = new cardHandler( $G_PGLOB->DBCA ) ;
        $result_data	  = $cardHandler->searchCardRKD( $_REQUEST ) ;

        //查询结果
        $result_array = array(
            'page'  => $_REQUEST['page'],
            'total' => $_REQUEST['count'],
            'rows'  => array()
        );

        if ($result_data) {
            foreach($result_data as &$cardrkd) {

                //操作字符串
                $operatorString = '';

                //代金卡状态字符串
                $status = '';

                if( $cardrkd['status'] == 0 ){
                    $operatorString .= '<a href="javascript: void(0);" onclick="storeCard.showEdit(' . $cardrkd['productID'] . ',' . $cardrkd['rkdID'] . ',' . $cardrkd['startID'] . ',' . $cardrkd['endID'] . ',\'' . $cardrkd['timeStamp'] . '\')">修改</a>&nbsp;'.
                        '<a href="javascript: void(0);" onclick="storeCard.audit(' . $cardrkd['rkdID'] . ')">审核</a>';

                    $status .= '待审核';
                }

                if( $cardrkd['status'] == 1 ){
                    $status .= '已入库';
                }

                $result_array['rows'][] = array(
                    'id' 	=> $cardrkd['rkdID'],
                    'cell' 	=> array(
                        $operatorString ,
                        $cardrkd['rkdID'] ,
                        $cardrkd['startID'],
                        $cardrkd['endID'],
                        $status
                    )
                );
            }
        }

        echo json_encode( $result_array );

    }

    /**
     * 更新或保存入库单
     */
    private function editCardRKD() {

        global $G_PGLOB;

        /**
         * 代金卡入库单状态：0:待审核   1：已入库
         */

        $_REQUEST['operator'] = $G_PGLOB->currentUserID;

        //编辑时，检查代金卡状态
        if( !empty($_REQUEST['rkdID']) ){

            //接收页面传过来的timeStamp
            $timeStamp = $_REQUEST['timeStamp'];

            //查询代金卡入库单信息
            $card_rkd = new entity('card_rkd');
            $card_rkd->rkdID = $_REQUEST['rkdID'];

            if( !$G_PGLOB->DBCA->fetch( $card_rkd ) ){

                echo json_encode( array( 'code'=>404 , 'message'=>'该入库单已被删除或不存在') );
                return;

            }

            if(  $card_rkd->status == 1 ){

                echo json_encode( array( 'code'=>404 , 'message'=>'该入库单状态已改变') );
                return;

            }

            if(  $card_rkd->timeStamp != $timeStamp ){

                echo json_encode( array( 'code'=>404 , 'message'=>'该入库单已更新，请刷新页面') );
                return;

            }

            $_REQUEST['startID'] = $_REQUEST['startID_edit'];
            $_REQUEST['endID']   = $_REQUEST['endID_edit'];

        }

        //调用卡管理类中的更新代金卡入库单方法
        $cardHandler = new cardHandler( $G_PGLOB->DBCA ) ;
        $result_array = $cardHandler->editCardRKD( $_REQUEST ) ;

        echo json_encode($result_array);

    }

    /**
     * 删除代金卡入库单
     *
     */
    private function delCardRKD () {

        global $G_PGLOB;

        //接收参数
        $delArr = $_REQUEST['delArr'];

        //调用卡管理类中的删除代金卡入库单方法
        $cardHandler  = new cardHandler( $G_PGLOB->DBCA ) ;
        $result_array = $cardHandler->delCardRKD( $delArr ) ;

        echo json_encode($result_array);

    }

    //审核代金卡入库单
    public function auditCardRKD(){

        global $G_PGLOB;

        //调用卡管理类中的审核代金卡入库单方法
        $cardHandler = new cardHandler( $G_PGLOB->DBCA ) ;
        $result_array 		 = $cardHandler->auditCardRKD( $_REQUEST['rkdID'] ) ;

        echo json_encode($result_array);

    }

    //代金卡详情列表
    function cardDetailList(){

        global $G_PGLOB;

        //查询条件
        $cardHandler = new cardHandler( $G_PGLOB->DBCA ) ;
        $result_data	  = $cardHandler->searchCardDetail( $_REQUEST ) ;

        //查询结果
        $result_array = array(
            'page'  => $_REQUEST['page'],
            'total' => $_REQUEST['count'],
            'rows'  => array()
        );

        if ($result_data) {
            foreach($result_data as &$cardDetail) {

                if( $cardDetail['cardType'] == 1 ){
                    $cardType = '虚拟卡';
                }elseif ( $cardDetail['cardType'] == 2 ){
                    $cardType = '线上销售实体卡';
                }elseif ( $cardDetail['cardType'] == 3 ){
                    $cardType = '线下销售实体卡';
                }

                if( $cardDetail['cardStatus'] == 1 ){
                    $cardStatus = '已生产';
                }elseif ( $cardDetail['cardStatus'] == 2 ){
                    $cardStatus = '入库申请';
                }elseif ( $cardDetail['cardStatus'] == 3 ){
                    $cardStatus = '已入库';
                }elseif ( $cardDetail['cardStatus'] == 4 ){
                    $cardStatus = '申领申请';
                }elseif ( $cardDetail['cardStatus'] == 5 ){
                    $cardStatus = '发卡';
                }elseif ( $cardDetail['cardStatus'] == 6 ){
                    $cardStatus = '使用';
                }elseif ( $cardDetail['cardStatus'] == 7 ){
                    $cardStatus = '<font color="red">作废</font>';
                }

                if( $cardDetail['cardStatus'] != 7 ){
                    $operatorString = '<a href="javascript: void(0);" onclick="cardDetail.cancel(' . $cardDetail['cardID'] . ')">作废</a>';
                }else{
                    $operatorString = '';
                }

                $result_array['rows'][] = array(
                    'id' 	=> $cardDetail['cardID'],
                    'cell' 	=> array(
                        $operatorString ,
                        $cardDetail['cardID'] ,
                        $cardDetail['productID'],
                        $cardDetail['rkdID'],
                        $cardDetail['applyID'],
                        $cardDetail['faceValue'],
                        $cardDetail['discount'],
                        sprintf( "%.2f" , ($cardDetail['discount'] * $cardDetail['faceValue']) ),
                        $cardDetail['useNumber'],
                        $cardDetail['usedNumber'],
                        $cardDetail['sendCardTime'],
                        $cardType,
                        $cardStatus
                    )
                );
            }
        }

        echo json_encode( $result_array );

    }

    //批量禁用代金卡
    function batchChangeCardStat(){

        global $G_PGLOB;

        //接收参数
        $delArr = $_REQUEST['delArr'];

        //调用卡管理类中的批量禁用代金卡方法
        $cardHandler = new cardHandler( $G_PGLOB->DBCA ) ;
        $result_array 		 = $cardHandler->batchChangeCardStat( $delArr ) ;

        echo json_encode($result_array);

    }

    //回滚卡状态
    function cardRollBack(){

        global $G_PGLOB;

        //调用卡管理类中的回滚卡状态方法
        $cardHandler = new cardHandler( $G_PGLOB->DBCA ) ;
        $result_array 		 = $cardHandler->changeCardStat( $_REQUEST['cardPwd'] ) ;

        echo json_encode($result_array);

    }


}